package com.sunmouse.toolkit.fragment.anim;


import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.sunmouse.toollib.utils.LogUtils;
import com.sunmouse.toolkit.R;
import com.sunmouse.toolkit.fragment.BaseFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class ObjectAnimFragment extends BaseFragment {


    @BindView(R.id.anim_btn)
    Button animBtn;
    Unbinder unbinder;
    private ValueAnimator animator;

    public ObjectAnimFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_object_anim, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void init() {
        this.animBtn.setOnClickListener(this);

        animator = ValueAnimator.ofFloat(1.0f, 0.5f, 1.0f);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float scaleFactor = (float) animation.getAnimatedValue();//get calculated value
                animBtn.setScaleX(scaleFactor);
                animBtn.setScaleY(scaleFactor);
                LogUtils.d("-----" + animation.getAnimatedValue());
                LogUtils.d("-----width=" + animBtn.getWidth());
            }
        });

        animator.setDuration(2 * 1000);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.anim_btn:
                animator.start();
                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
